package com.mdp.sys.pub.service;

import com.mdp.core.api.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

public abstract  class RedisCacheService<T>  implements Cache<T> {
	
	@Autowired
	RedisTemplate redisTemplate;
	 

	@Override
	public   void put(String key, T o) {
		redisTemplate.opsForHash().put(getCacheKey(), key, o);  
		
	}

	@Override
	public   T get(String key) {
		
		return  (T) redisTemplate.opsForHash().get(getCacheKey(), key);
	}

	@Override
	public void remove(String key) {
		redisTemplate.opsForHash().delete(getCacheKey(), key); 
	}

	@Override
	public boolean containsKey(String key) {
		
		return redisTemplate.opsForHash().hasKey(getCacheKey(), key);
	}

	@Override
	public void refresh() {
		
		
	}

	@Override
	public boolean containsValue(T value) {
		
		return false;
	}
	
	public abstract  String getCacheKey() ;
	
	 
	@Override
	public boolean expire(long milliseconds) {
		
		return redisTemplate.expire(getCacheKey(), milliseconds, TimeUnit.MILLISECONDS);
	}
	@PostConstruct 
	public boolean expire() {
		
		return redisTemplate.expire(getCacheKey(), 120000, TimeUnit.MILLISECONDS);
	}
}